<template>
  <div class="app-container">
    <div
      ref="main"
      class="chart-container"
    />
  </div>
</template>
<script>
import { classesList } from '@/api/classes'
const echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/bar')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: {
        size: 20,
        current: 1,
        total: 1
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.initCharts()
  },
  methods: {
    initCharts() {
      this.chart = echarts.init(this.$refs.main)
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line'
          }
        },
        color: ['#3398DB'],
        dataset: {
          source: [
            ['score', 'amount', 'product'],
            [89.3, 1, '支付宝'],
            [57.1, 2, 'Milk Tea'],
            [74.4, 3, 'Cheese Cocoa'],
            [50.1, 4, 'Cheese Brownie'],
            [89.7, 5, 'Matcha Cocoa'],
            [68.1, 6, 'Tea'],
            [19.6, 7, 'Orange Juice'],
            [10.6, 8, 'Lemon Juice'],
            [32.7, 9, 'Walnut Brownie']
          ]
        },
        grid: { containLabel: true },
        xAxis: { name: 'amount' },
        yAxis: { type: 'category' },
        series: [
          {
            name: '访问时长',
            type: 'bar',
            barWidth: '60%',
            encode: {
              x: 'amount',
              y: 'product'
            }
          }
        ]
      })
    },
    fetchData() {
      this.listLoading = true
      classesList(this.page).then(response => {
        this.list = response.body
        this.listLoading = false
        this.page.total = response.totalCount
      })
    }
  }
}
</script>
<style scoped>
  .chart-container{
    position: relative;
    width: 100%;
    height: calc(100vh - 84px);
  }
</style>
